package com.fmsz.xmly.adapters.broadcast;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.broadcast.BroadCastEntity;
import com.fmsz.xmly.entitys.broadcast.LocalRadiosEntity;
import com.fmsz.xmly.entitys.broadcast.TopRadiosEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private int tag;
    public static final int TAG1 = 0;
    public static final int TAG2 = 1;
    private List<LocalRadiosEntity> localRadiosEntities;
    private List<TopRadiosEntity> topRadiosEntities;

    public ListViewAdapter(Context context, int tag) {
        this.context = context;
        this.tag = tag;
        localRadiosEntities = new ArrayList<>();
        topRadiosEntities = new ArrayList<>();
    }

    public void addAll(BroadCastEntity entity){
        localRadiosEntities.addAll(entity.getData().getLocalRadios());
        topRadiosEntities.addAll(entity.getData().getTopRadios());
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return tag==TAG1?localRadiosEntities.size():topRadiosEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return localRadiosEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.find_broad_lv_item,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if (tag==TAG1) {
            holder.tv1.setText(localRadiosEntities.get(position).getName());
            holder.tv2.setText("正在直播："+localRadiosEntities.get(position).getProgramName());
            holder.tv3.setText(localRadiosEntities.get(position).getProgramScheduleId());
            Picasso.with(context).load(localRadiosEntities.get(position).getCoverSmall()).into(holder.icon);
        }else{
            holder.tv1.setText(topRadiosEntities.get(position).getName());
            holder.tv2.setText("正在直播："+topRadiosEntities.get(position).getProgramName());
            holder.tv3.setText(topRadiosEntities.get(position).getProgramScheduleId());
            Picasso.with(context).load(topRadiosEntities.get(position).getCoverSmall()).into(holder.icon);
        }
        return convertView;
    }
    public static class ViewHolder{
        private View convertView;
        private ImageView icon;
        private TextView tv1;
        private TextView tv2;
        private TextView tv3;

        public ViewHolder(View convertView) {
            this.convertView = convertView;
            initView(convertView);
        }

        private void initView(View convertView) {
            icon = ((ImageView) convertView.findViewById(R.id.broad_lv_icon));
            tv1 = ((TextView) convertView.findViewById(R.id.broad_tv_top));
            tv2 = ((TextView) convertView.findViewById(R.id.broad_tv_mid));
            tv3 = ((TextView) convertView.findViewById(R.id.broad_tv_end));
        }
    }
}
